<?php
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Form\CreateBillForm;
use Zend\Session\Container;;
use Zend\Mvc\MvcEvent;

class BillController extends AbstractActionController{
	protected $session;
    protected $view;
    protected $em;
    protected $authService;

    public function __construct (){
        $this->session = new Container();
        $this->view = new ViewModel();
        $this->view->controller = 'bill';
        $this->view->publicPath = PUBLIC_PATH;
        $this->em = $this->getEventManager();
        $this->em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'),100);
        echo '<script> var controllername="bill"; </script>';
    }

    public function preDispatch(){
        if(!$this->getAuthService()->hasIdentity())
            $this->_redirect()->toRoute(NULL,array('controller'=>'auth','action'=>'login'));
        $username = $this->getAuthService()->getStorage()->read();
        $this->layout('layout/admin');
        $this->layout()->username = $username;
    }

    public function getAuthService(){
        if(!$this->authService){
            $this->authService = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authService;
    }

    public function indexAction(){
    	// trang quản lý hóa đơn, hiển thị tất cả hóa đơn
        $billTable = $this->getServiceLocator()->get('Admin\Model\BillTable');
        $bills = $billTable->fetchAll();
        $this->view->bills = $bills;
        if($this->session->offsetExists('thongbao')){
            $this->view->thongbao = $this->session->offsetGet('thongbao');
            $this->session->offsetUnset('thongbao');
        }
        return $this->view;
    }

     public function createBillAction(){
        // tạo hóa đơn cho sản phẩm
            $id = $this->params()->fromRoute('id');
            $billTable = $this->getServiceLocator()->get('Admin\Model\BillTable');
            $form = new CreateBillForm();
            $this->view->form = $form;
            $productTable = $this->getServiceLocator()->get('Admin\Model\AdminProductTable');
            $items = $productTable->getSimpleInform();
            $this->view->items = $items;
            $this->view->currentItem = NULL;
            if($id!=NULL){
                // echo "Đã có thông tin sản phẩm";
                $this->view->currentItem = $productTable->getProduct($id);
            }
        return $this->view;
    }

    public function uploadAction(){
        $arrParams = $this->getRequest()->getPost()->toArray();
        if($arrParams!=NULL){
            $billTable = $this->getServiceLocator()->get('Admin\Model\BillTable');
            $result = $billTable->saveBill($arrParams);
            if($result){
                $this->session->offsetSet('thongbao','Thêm hóa đơn thành công');
                // sau khi thêm hóa đơn tiến hành cập nhật số lượng hàng đã bán
                $productTable = $this->getServiceLocator()->get('Admin\Model\AdminProductTable');
                $result2 = $productTable->soldCounters($arrParams['maHang'],$arrParams['soLuong']);
            }else{
                $this->session->offsetSet('thongbao','Thêm hóa đơn thất bại');
            }
            $this->redirect()->toRoute(NULL,array('controller'=>'bill','action'=>'index'));
        }else{
            $this->redirect()->toRoute(NULL,array('controller'=>'bill','action'=>'create-bill'));
        }
    }

    public function deleteAction(){
        $post = $this->getRequest()->getPost()->toArray();
        if($post!=NULL){
            $billTable = $this->getServiceLocator()->get('Admin\Model\BillTable');
            $result = $billTable->deleteBill($post['id']);
            if($result)
                $this->session->offsetSet('thongbao','Xóa hóa đơn thành công');
            else
                $this->session->offsetSet('canhbao','Có lỗi trong quá trình thực hiện');
        }else{
            $id = $this->params()->fromRoute('id');
            $this->view->id = $id;
            return $this->view;
        }
        $this->redirect()->toRoute(NULL,array('controller'=>'bill','action'=>'index'));
        return $this->view;
    }

    public function editAction(){
        return $this->view;
    }
}